<?php
namespace app\api\controller;

use app\home\model\News as ModelNews;
use app\home\model\ShortLink;
use PullWord\Facade\PullWord;
use tcwei\smallTools\GetImgSrc;
use think\facade\Db;
use think\facade\Request;

class News
{
    public function index()
    {
        $page = request()->param('page') - 1;
        $page *= 10;
        $sql = "SELECT id FROM stpc_qzz_news ORDER BY id DESC LIMIT $page, 1";
        $last_news_id = Db::query($sql)[0]['id'];
        $temp = ModelNews::field(['id', 'title', 'introduce', 'thumb', 'hot_order', 'addtime', 'keywords'])->where('id', '<=', $last_news_id)->order('id desc')->limit(0, 10)->select();
        $news = [];
        foreach($temp as $n){
            $temp_n = [
                'id' => $n->id,
                'title' => $n->title,
                'introduce' => $n->introduce,
                'thumb' => $n->thumb,
                'hot_order' => $n->hot_order,
                'addtime' => $n->getData('addtime'),
                'keywords' => $n->keywords,
            ];
            array_push($news, $temp_n);
        }
        return json([
            'status'=>200,
            'news'=>$news,
            'last_id'=>$last_news_id,
            'count'=>ModelNews::count(),
        ]);
    }

    public function add()
    {
        $data = Request::param();
        try {
            if($data['introduce'] == ""){
                $content = strip_tags($data['content']);
                $data['introduce'] = mb_substr($content, 0, 40).'...';
            }
            if ($data['thumb'] == ""){
                $data['thumb'] = GetImgSrc::src($data['content']);
            }
            if ($data['keywords'] == ""){
                $title = $data['title'];
                $result = (array)json_decode(PullWord::pull($title)->toJson()->get());
                $str = "";
                foreach($result as $i => $r){
                    $str .= $r->t;
                    if ($i != count($result) - 1){
                        $str .= ', ';
                    }
                }
                $data['keywords'] = $str;
            }
        }catch(\Exception $e){
            return json("处理信息时出错:".$e->getMessage(), 500);
        }

        try{
            $hash = md5($data['title'].microtime(true));
            $hash = substr(base64_encode($hash), 0, 6);
            $data['hash_str'] = $hash;
            $news = new ModelNews();
            $data['addtime'] = $data['updatetime'] = date("Y:m:d H:i:s");
            $news->save($data);
            $id = $news->id ;
            $st = new ShortLink;
            $st->save([
                'c_name' => 'Nd',
                'hash_str' => $data['hash_str'],
                'p_name' => 'page',
                'p_id' => $id,
            ]);

            return json([
                'news'=>ModelNews::find($news->id)->hidden(['content', 'addtime', 'updatetime', 'hash_str']),
            ]);
        }catch(\Exception $e){
            return json("添加短连接时出错:".$e->getMessage(), 500);
        }
        
       
    }

    public function edit(){
        $data = request()->param();
        $news = ModelNews::find($data['id']);

        return json([
            'status'=>$news->save($data),
            'news'=>$news->hidden([
                'addtime',
                'updatetime',
                'hash_str',
                'content',
            ]),
        ]);
    }

    public function del(){
        $news = ModelNews::field(['hash_str', 'id'])->find(request()->param('id'));
        ShortLink::where('hash_str', $news->hash_str)->delete();
        return json(ModelNews::destroy($news->id));
    }

    public function getNews(){
        return json(ModelNews::field(['id', 'title, introduce, thumb, keywords, content'])->find($_GET['id']));
    }

}
